package com.szh.wechat.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.szh.wechat.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.szh.wechat.model.User;
import com.szh.wechat.service.UserService;

import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
public class UserController {

	@Autowired
	private UserService userService;
	@Resource
	MessageService messageService;

	@GetMapping("/users")
	public ResponseEntity<List<User>> getAllUsers() {
		List<User> users = userService.getAllUsers();
		return ResponseEntity.ok(users);
	}

	@GetMapping("/curUser")
	public ResponseEntity<User> getCurUser(@CookieValue("userId") String userId) {
		User user = userService.selectByUserId(userId);
		return ResponseEntity.ok(user);
	}

	/**
	 * 给某个用户发送消息
	 * @param json
	 * @param request
	 * @return
	 */
	@PostMapping("/pushToUser")
	public String pushToUser(@RequestBody JSONObject json, HttpServletRequest request){
		return messageService.pushMsgToUser(json,request);
	}

	/**
	 * 给所有用户发送消息
	 * @param json
	 * @param request
	 * @return
	 */
	@PostMapping("/pushToAll")
	public String pushToAll(@RequestBody JSONObject json, HttpServletRequest request){

		return messageService.pushToAll(json,request);
	}



}
